//
//  MSTradeConfirmContactsCell.m
//  SocietySale
//
//  Created by Mike on 16/11/14.
//  Copyright © 2016年 Mike. All rights reserved.
//

#import "MSTradeConfirmContactsCell.h"

@interface MSTradeConfirmContactsCell ()<UITextFieldDelegate>
@property (nonatomic, strong) GXButton    *phoneBtn;
@property (nonatomic, strong) UITextField *shortInputField;
@property (nonatomic, strong) UITextField *longInputField;
@property (nonatomic, strong) UIView      *carryView;
@end

@implementation MSTradeConfirmContactsCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self initView];
    }
    return self;
}

- (void)initView {
    self.textLabel.font = HeitiFont(14);
    self.textLabel.textColor = [UIColor blackColor];
    self.textLabel.textAlignment = NSTextAlignmentLeft;
    
    self.inputField = [[UITextField alloc] initWithFrame:CGRectMake(65, 0, SCREENWIDTH - 70 ,44)];
    self.inputField.textColor = [UIColor blackColor];
    self.inputField.font = HeitiFont(14);
    self.inputField.clearButtonMode = UITextFieldViewModeWhileEditing;
    [self.contentView addSubview:self.inputField];
    
    self.unitLabel = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH-32, 0, 20, 44)];
    self.unitLabel.font = HeitiFont(14);
    self.unitLabel.textColor = kLightBlackColor;
    self.unitLabel.text = @"万";
    self.unitLabel.textAlignment = NSTextAlignmentRight;
    [self.contentView addSubview:self.unitLabel];
    self.unitLabel.hidden = YES;
    
    _horizonLineView = [[UIView alloc] initWithFrame:CGRectMake(12, 43.4, SCREEN_WIDTH-24, 0.6)];
    _horizonLineView.backgroundColor = kNormalGray;
    [self.contentView addSubview:_horizonLineView];
    
    self.carryView = [[UIView alloc] initWithFrame:CGRectMake(62, 0, SCREEN_WIDTH-150, 43)];
    self.carryView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:self.carryView];
    self.carryView.hidden = YES;
    
    self.shortInputField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 60 ,44)];
    self.shortInputField.textColor = [UIColor blackColor];
    self.shortInputField.font = HeitiFont(14);
    self.shortInputField.placeholder = @"区号";
    self.shortInputField.delegate = self;
    self.shortInputField.keyboardType = UIKeyboardTypeNumberPad;
    [self.carryView addSubview:self.shortInputField];
    
    _shortLine =[[UIView alloc] initWithFrame:CGRectMake(60, 10, 0.6, 24)];
    _shortLine.backgroundColor = kNormalGray;
    [self.carryView addSubview:_shortLine];

    self.longInputField = [[UITextField alloc] initWithFrame:CGRectMake(75, 0, SCREENWIDTH - 217 ,44)];
    self.longInputField.textColor = [UIColor blackColor];
    self.longInputField.font = HeitiFont(14);
    self.longInputField.placeholder = @"电话号码";
    self.longInputField.delegate = self;
    self.longInputField.keyboardType = UIKeyboardTypeNumberPad;
    [self.carryView addSubview:self.longInputField];
    
    self.phoneBtn = [[GXButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH-82, 10, 70, 24)];
    self.phoneBtn.titleLabel.font = HeitiFont(13);
    [self.phoneBtn setTitle:@"输入固话" forState:UIControlStateNormal];
    [self.phoneBtn setTitle:@"输入手机号" forState:UIControlStateSelected];
    [self.phoneBtn setTitleColor:kLightGrayColor forState:UIControlStateNormal];
    self.phoneBtn.layer.cornerRadius = 3;
    self.phoneBtn.layer.borderWidth = 1;
    self.phoneBtn.layer.borderColor = kLightGrayColor.CGColor;
    [self.phoneBtn addTarget:self action:@selector(phoneClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.phoneBtn];
    self.phoneBtn.hidden = YES;
}

- (void)setData:(DOKeyValue *)data {
    _data = data;
    self.textLabel.text = [NSString stringWithFormat:@"%@",_data.key];
    self.inputField.placeholder = _data.placeHolder;
    self.inputField.text = _data.value;
    
    CGSize keySize = [HDUtil sizeWithString:_data.key font:HeitiFont(14) size:CGSizeMake(SCREEN_WIDTH-24, 44)];
    
    if ([_data.key isEqualToString:@"电话"]) {
        self.phoneBtn.hidden = NO;
        self.inputField.frame = CGRectMake(keySize.width+35, 0, SCREENWIDTH - keySize.width - 45 - 75,44);
        self.inputField.keyboardType = UIKeyboardTypeNumberPad;

        NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
        NSString *state = [ud objectForKey:@"phoneState"];
        if ([state isEqualToString:@"y"]) {
            self.carryView.hidden = NO;
            [self.contentView bringSubviewToFront:self.carryView];
            self.phoneBtn.selected = YES;
        }
        else{
            self.carryView.hidden = YES;
            [self.contentView bringSubviewToFront:self.inputField];
            self.phoneBtn.selected = NO;
        }
    }
    else{
        self.phoneBtn.hidden = YES;
        self.carryView.hidden = YES;

        self.inputField.frame = CGRectMake(keySize.width+35, 0, SCREENWIDTH - keySize.width - 45 ,44);
        
        if ([_data.key isEqualToString:@"价格"]){
            self.inputField.keyboardType = UIKeyboardTypeNumberPad;
        }
        else{
            self.inputField.keyboardType = UIKeyboardTypeDefault;
        }
    }
}

- (void)phoneClick:(GXButton*)btn{
    btn.selected = !btn.selected;
    if (btn.selected) {
        self.carryView.hidden = NO;
        [self.contentView bringSubviewToFront:self.carryView];
    }
    else{
        self.carryView.hidden = YES;
        [self.contentView bringSubviewToFront:self.inputField];
    }

    NSString *state = btn.selected?@"y":@"n";
    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
    [ud setObject:state forKey:@"phoneState"];
    [ud synchronize];
    
    BOOL isSelected = [state isEqualToString:@"y"];
    if ([self.delegate respondsToSelector:@selector(areaPhoneNumEndEditIsArea: prefix: suffix:)]) {
        [self.delegate areaPhoneNumEndEditIsArea:isSelected prefix:self.shortInputField.text suffix:self.longInputField.text];
    }
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
    if ([self.delegate respondsToSelector:@selector(areaPhoneNumEndEditIsArea: prefix: suffix:)]) {
        [self.delegate areaPhoneNumEndEditIsArea:YES prefix:self.shortInputField.text suffix:self.longInputField.text];
    }
    return YES;
}

@end
